package com.tjtt.tdm.system.action;

import java.io.PrintWriter;
import java.math.BigDecimal;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import antlr.StringUtils;

import com.tjtt.common.utils.StringUtil;
import com.tjtt.tdm.base.BaseAction;
import com.tjtt.tdm.base.PageResult;
import com.tjtt.tdm.flow.service.IFlowformService;
import com.tjtt.tdm.flow.service.IFlowtypeService;
import com.tjtt.tdm.system.bean.SysTips;
import com.tjtt.tdm.system.service.ITipsService;
/**
 * 系统设定管理
 * 
 * @author 宿馨月
 * 
 */
public class TipsAction extends BaseAction{
	private ITipsService tipsService;

	public ITipsService getTipsService() {
		return tipsService;
	}

	public void setTipsService(ITipsService tipsService) {
		this.tipsService = tipsService;
	}
	
	public void getSysTips() throws Exception{
		setContentType();
		PrintWriter out = getResponse().getWriter();
		try{
			//String tipsContent = "<span style='color:blue;cursor:pointer;' onclick=changeRight('')>仪器设备检定过期提醒</span><br>";
			
			WebApplicationContext factory = WebApplicationContextUtils.getRequiredWebApplicationContext(getSession().getServletContext());
			IFlowtypeService flowtypeService =  (IFlowtypeService)factory.getBean("flowtypeService");
			String strSql = "select count(1) from flow_task_info t where t.next_users_id like '%,"+this.getUserId()+",%'  and t.flow_state_id<>'end' ";
			List lists = flowtypeService.findAllBySql(strSql);
			String tipsContent ="<span style='color:blue;cursor:pointer;' onclick=changeRight('/kltdm/workflow/haslaunched_worklist.jsp')>代办任务数（"+lists.get(0)+"）</span><br>";
			out.print(tipsContent);	
		} catch (Exception e) {
			e.printStackTrace();
			out.print("error");	
		}
	}
	/**
	 * 数组转字符串
	 * @param stringList
	 * @return
	 */
	 public static String listToString(List stringList){
	        if (stringList==null) {
	            return null;
	        }
	        StringBuilder result=new StringBuilder();
	        boolean flag=false;
	        for (int i = 0; i < stringList.size(); i++) {
	        	 if (flag) {
		                result.append(",");
		            }else {
		                flag=true;
		            }
	        	 String string = String.valueOf(stringList.get(i));
	        	 result.append(string);
			}
	        return result.toString();
	    }
	public void queryTips(){
		try{
			Map filterMap = getPagerMap();
			String[] viewArray = {"id","tipContent","tipSql","url","state"};
			//查询所有的数据
			PageResult result =  tipsService.queryTips(filterMap);
			String returnStr = getPagerJson(result,viewArray);
			setContentType();
			PrintWriter out = getResponse().getWriter();
			out.print(returnStr);	
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	public void saveTip() throws Exception {
//		setContentType();
		getResponse().setCharacterEncoding("utf-8");         
		getResponse().setContentType("text/html; charset=utf-8");
		PrintWriter out = getResponse().getWriter();
		try {
			String tipContent = getRequest().getParameter("tipContent");
			String tipSql = getRequest().getParameter("tipSql");
			String url = getRequest().getParameter("url");
			String type = getRequest().getParameter("type");
			SysTips tip = new SysTips();
			tip.setState(new BigDecimal("1"));
			tip.setTipContent(tipContent);
			tip.setTipSql(tipSql);
			tip.setUrl(url);
			tip.setType(type);
			tipsService.saveTip(tip);
			out.print("添加成功！");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			out.print("添加失败");
		}
	}
	public String gotoEdit(){
		try {
			String id = getRequest().getParameter("id");
			SysTips tip = tipsService.getOneTip(id);
			getRequest().setAttribute("tip", tip);
			return "edit";
		} catch (Exception e) {
			e.printStackTrace();
			return "error";
		}
	}
	public void editTip() throws Exception {
//		setContentType();
		getResponse().setCharacterEncoding("utf-8");         
		getResponse().setContentType("text/html; charset=utf-8");
		PrintWriter out = getResponse().getWriter();
		try {
			String tipContent = getRequest().getParameter("tipContent");
			String tipSql = getRequest().getParameter("tipSql");
			String url = getRequest().getParameter("url");
			String type = getRequest().getParameter("type");
			String id = getRequest().getParameter("id");
			SysTips tip = new SysTips();
			tip.setState(new BigDecimal("1"));
			tip.setTipContent(tipContent);
			tip.setTipSql(tipSql);
			tip.setUrl(url);
			tip.setType(type);
			tip.setId(Long.parseLong(id));
			tipsService.updateTip(tip);
			out.print("保存成功！");
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
			out.print("保存失败");
		}
	}
	
	public void deleteTip() throws Exception {
		PrintWriter out = getResponse().getWriter();
		try {
			String ids = getRequest().getParameter("ids");
			if (ids != null && !ids.isEmpty()) {
				tipsService.deleteTip(ids);
			}
			out.print("{\"ajaxResult\":\"success\"}");
		} catch (Exception e) {
			e.printStackTrace();
			out.print("{\"ajaxResult\":\"error\"}");
		}
	}
}
